package com.example.codegen;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Optional;

public class CodeGenGo {
    public static void main(String[] args) {

        String kLassPath = "src/main/resources/classDB.txt";
        String dSLProgramPath = "src/main/resources/programDB.txt";
        String outputJavaDir = "src/main/resources/";
        String outputFilePath = outputJavaDir + "BD.go";
//        String outputFilePath =  "/tmp/array/BD.go";
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath));
            writer.write("package main");
            writer.newLine();
            writer.write("import (\n" +
//                    "\t\"fmt\"\n" +
                    "\t\"sync\"\n" +
                    ")\n");
            BufferedReader klassReader = new BufferedReader(new FileReader(kLassPath));
            BufferedReader dSLProgramReader = new BufferedReader(new FileReader(dSLProgramPath));
            // test is klassReader is empty without readline
            Optional<Klass> klass = Optional.empty();
            do {
                klass = Parser.parserKlass(klassReader);
                if (klass.isPresent()) {
                    Klass k = klass.get();
                    // fix: go don't support $
                    writer.write(k.toGo().replace("$", "_"));
                    writer.newLine();
                }
            } while (klass.isPresent());
            writer.newLine();

            Optional<Method> program = Optional.empty();
            do {
                program = Parser.parserMethod("", dSLProgramReader);
                if (program.isPresent()) {
                    Method m = program.get();
                    // fix: go don't support $
                    writer.write(m.toGo().replace("$", "_"));
                    writer.newLine();
                }
            } while (program.isPresent());
            writer.newLine();
            writer.close();
            klassReader.close();
            dSLProgramReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
